<?php

defined('BASEPATH') OR exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
|  消息模型
|@author heyu
|--------------------------------------------------------------------------
*/
class Message extends MY_Controller {

    /**
     * 消息模型
     * @var ConsumerMessageModel
     */
    public $mMessage;

    public function __construct(){
        parent::__construct();
        $this->loadModel('ConsumerMessageModel','mMessage','consumer');
    }

    /**
     * 首页
     */
    public function index(){
        $data = array(
            'order' => $this->mMessage->getMessageTotal($this->userId, 2),
            'system' => $this->mMessage->getMessageTotal($this->userId, 1),
            'title' => '消息',
            'active' => 'message'
        );
        $this->view('message/index', $data);
    }

    /**
     * 订单消息
     */
    public function order(){
        $list = $this->mMessage->appGetList($this->userId, 3,1);
        $data = array(
            'list' => $list,
            'title' => '订单消息'
        );
        $this->view('message/order', $data);
    }

    /**
     * 系统消息
     */
    public function system(){
        $list = $this->mMessage->appGetList($this->userId, 1, 1);
        $data = array(
            'list' => $list,
            'title' => '系统消息'
        );
        $this->view('message/system', $data);
    }

    /**
     * 获取消息详情
     */
    public function details(){
        //表单验证
        $vConf = array(
            array('message_id', '消息id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('message_id' => get('message_id')));

        $messageId = (int)get('message_id');
        if (!$messageId){
            show_404();
        }
        $this->loadModel('ConsumerMessageModel','mMsg','consumer');

        $info = $this->mMessage->getInfo($messageId,$this->userId);
        if (!$info){
            show_404();
        }
        if ( ! $this->mMessage->getMsgStatus($messageId, $this->userId)){
            //阅读信息
            $data = array(
                'consumer_id'=>$this->userId,
                'message_id'=>$messageId,
                'is_delete'=>0,
                'addtime'=>time(),
            );
            if ( ! $this->mMessage->saveStatus($data,$messageId,$this->userId)){
                //$this->appReturn(0,'获取失败');
            }
        }
        $data = array(
            'info' => $info,
            'title' => '消息详情'
        );
        $this->view('message/details', $data);

//        $this->ajaxReturn(1, '', $html);

    }

    /**
     * 删除消息
     */
    public function rmMsg(){
        //表单验证
        $vConf = array(
            array('message_id', '消息id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);

        $messageId = post('message_id');

        $this->loadModel('ConsumerMessageModel','mMsg','consumer');

        $tA = $this->mMsg->getInfo($messageId,$this->userId);
        if (! $tA){
            $this->appReturn(0,'数据为空');
        }

        if ($tA['type'] == 2){
            //个人信息，真删除
            $this->db->where(array('id'=>$messageId,'consumer_id'=>$this->userId));
            if ( ! $this->db->delete($this->mMsg->tbMessage)){
                $this->appReturn(0,'删除失败');
            }

            //删除浏览状态
            $this->db->where(array('message_id'=>$messageId,'consumer_id'=>$this->userId));
            if (! $this->db->delete($this->mMsg->tbMessageViewed)){
                $this->appReturn(0,'删除失败');
            }
        }else{

            //假删除
            if ( ! $this->mMsg->saveStatus(array('is_delete'=>2),$messageId,$this->userId)){
                $this->appReturn(0,'删除失败');
            }
        }

        $this->appReturn(1,'已删除');
    }
}